package cn.xinfei.xdecision.engine.runner.context;

import cn.xinfei.xdecision.common.model.enginex.model.RuleHitModel;
import cn.xinfei.xdecision.common.model.enginex.model.RuleSetDecisionResultData;
import cn.xinfei.xdecision.common.utils.util.DateUtils;
import cn.xinfei.xdecision.constant.EngineResultEnum;
import cn.xinfei.xdecision.engine.runner.context.model.ChildNodeContextModel;
import cn.xinfei.xdecision.engine.runner.context.model.CreditNodeContextModel;
import cn.xinfei.xdecision.engine.runner.context.model.NodeContextModel;
import cn.xinfei.xdecision.engine.runner.context.model.TrackModel;
import cn.xinfei.xdecision.engine.runner.executor.model.StateTransmitModel;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.mongodb.core.mapping.Sharded;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Stack;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArrayList;
@Sharded(shardKey={"engineCode","createTime"})
@Data
@Slf4j
public class PipelineContext {
    private String decisionId;
    private String requestId;

    private String engineCode;
    private String engineName;
    private String engineVersion;
    private String scene;
    private String tpCode;
    private String mainDecisionId;
    private String parentDecisionId;
    private String championDecisionId;
    private String result;
    private String state;
    private String code;
    private long respond;
    private int organId;
    private String createTime = DateUtils.getCurrentStrDate();
    private String updateTime = DateUtils.getCurrentStrDate();
    private String branch = BranchTypeEnum.MAIN.name();//main主链路，challenge挑战组
    private Stack<CreditNodeContextModel> creditNodeStack = new Stack<CreditNodeContextModel>();//每遇到一个人行节点，就入栈；处理成功后，出栈
    private Stack<ChildNodeContextModel> childNodeNextNodeCodeStack = new Stack<ChildNodeContextModel>();//每遇到一个子决策流就入栈一个信息；处理成功后，出栈
    private Stack<NodeContextModel> nodeCodeStack = new Stack<NodeContextModel>();//每遇到一个节点就入栈一个信息；处理成功后，出栈

    private CopyOnWriteArrayList<RuleHitModel> hitResultList = new CopyOnWriteArrayList<>();//命中的规则列表
    private CopyOnWriteArrayList<RuleSetDecisionResultData> branchRejectInfo = new CopyOnWriteArrayList<>();//名中分支拒绝码

    private boolean invokeRemote = true;//是否对远程接口发起调用，测试的时候，基于测试接口入参改变。线上接口true
    private boolean persistence = true;//是否对快照持久化，测试的时候，基于测试接口入参改变。线上接口true
    private String sync = "ASYNC";//sync同步请求，async异步请求
    private Stack<String> ips=new Stack<>();//处理这条记录的及其ip

    /**
     * key：value(nodeCode,key,value)
     */
    private ConcurrentHashMap<String, Object> context = new ConcurrentHashMap<>();

    /**
     * 上游请求报文
     */
    private ConcurrentHashMap<String, Object> input = new ConcurrentHashMap<>();
    /**
     * 决策流最终输出
     */
    private Map<String, Object> output = new ConcurrentHashMap<>();

    /**
     * 节点轨迹追踪
     * engineCode:nodeCode
     */
    private ConcurrentHashMap<String, List<TrackModel>> track = new ConcurrentHashMap<String, List<TrackModel>>();


}
